import json

from app.extentions import db


class Commodity(db.Model):
    __tablename__ = 'commodity'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    category = db.Column(db.Integer, nullable=False)
    title = db.Column(db.String(255), nullable=False)
    price = db.Column(db.Float, nullable=False)
    desc = db.Column(db.String(255))
    image_urls = db.Column(db.JSON)

    def serialize(self):
        # 将Commodity序列化为python对象
        return {
            'id': self.id,
            'name': self.name,
            'category': self.category,
            'title': self.title,
            'price': self.price,
            'desc': self.desc,
            'image_urls': self.image_urls,
        }
